package it.polimi.traveldream.web.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import it.polimi.traveldream.dtos.DefaultPackageDTO;
import it.polimi.traveldream.dtos.ExcursionDTO;
import it.polimi.traveldream.dtos.FlightDTO;
import it.polimi.traveldream.dtos.HotelDTO;
import it.polimi.traveldream.dtos.ProductDTO;
import it.polimi.traveldream.exception.CityNotFoundException;
import it.polimi.traveldream.exception.ConsistencyException;
import it.polimi.traveldream.exception.NoIdException;
import it.polimi.traveldream.helper.Constants;
import it.polimi.traveldream.managers.CityManager;
import it.polimi.traveldream.managers.PackageManager;
import it.polimi.traveldream.managers.ProductManager;
import it.polimi.traveldream.web.helper.MessageStrings;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.FlowEvent;

@ManagedBean(name = "packageBean")
@ViewScoped
public class PackageBean {

	/*
	 * Costanti presenti in it.polimi.traveldream.helper nel progetto Client da
	 * utilizzare nella pagine JSF
	 */
	public final String FLIGHT = Constants.FLIGHT;
	public final String HOTEL = Constants.HOTEL;
	public final String EXCURSION = Constants.EXCURSION;

	@EJB
	private PackageManager packageMgr;

	@EJB
	private ProductManager productMgr;
	
	@EJB
	private CityManager cityMgr;

	private DefaultPackageDTO defaultPackageDTO;
	
	private List<ProductDTO> packageProducts;

	private List<FlightDTO> packageFlights;
	private List<HotelDTO> packageHotels;
	private List<ExcursionDTO> packageExcursion;
	
	private ProductDTODataModel productDTODataModel;
	 
	private HotelDTODataModel hotelDTODataModel;
	 
	private FlightDTODataModel flightDTODataModel;
	
	private ExcursionDTODataModel excursionDTODataModel;
	
	private ProductDTO clickedProductDTO;
	

	private static Logger logger = Logger
			.getLogger(PackageBean.class.getName());

	@PostConstruct
	private void init() {
		       
		defaultPackageDTO = new DefaultPackageDTO();
		
		packageProducts =  new ArrayList<ProductDTO>();
		
		packageFlights =  new ArrayList<FlightDTO>();
		packageHotels =  new ArrayList<HotelDTO>();
		packageExcursion =  new ArrayList<ExcursionDTO>();
		
		productDTODataModel = new ProductDTODataModel(productMgr.getAllProducts());
		flightDTODataModel = new FlightDTODataModel(productMgr.getAllFlight());
		hotelDTODataModel = new HotelDTODataModel(productMgr.getAllHotel());
		excursionDTODataModel = new ExcursionDTODataModel(productMgr.getAllExcursion());
			
		
	}

	public List<DefaultPackageDTO> getAllPackages() {
		List<DefaultPackageDTO> packageDTOs = packageMgr.getAll();

		return packageDTOs;
	}

	public String addDefaultPackage() {

		try {
			
			packageProducts.addAll(packageFlights);
			packageProducts.addAll(packageHotels);
			packageProducts.addAll(packageExcursion);
			
			defaultPackageDTO.setProductDTOs(packageProducts);
			packageMgr.addDefaultPackage(defaultPackageDTO);
			
			for(ProductDTO productDTO : defaultPackageDTO.getProductDTOs()){
				logger.info(productDTO.toString());
			}
						
		} catch (ConsistencyException e) {
			print("eccezione consistenza");
			addMessage("messages", "errore consistenza");

		}

		return "/admin/package/packageList?confirm=true&faces-redirect=true";

	}

	public String onFlowProcess(FlowEvent event) {
		logger.info("Current wizard step:" + event.getOldStep());
		logger.info("Next step:" + event.getNewStep());

		return event.getNewStep();
	}

	/*
	 * Helper Method
	 */

	private void print(String message) {
		System.out.print("PackageBean: " + message);
	}

	private void addMessage(String formId, String message) {
		FacesContext.getCurrentInstance().addMessage(formId,
				new FacesMessage(message));
	}

	/*
	 * Getter Setter
	 */

	public DefaultPackageDTO getDefaultPackageDTO() {
		return defaultPackageDTO;
	}

	public void setDefaultPackageDTO(DefaultPackageDTO defaultPackageDTO) {
		this.defaultPackageDTO = defaultPackageDTO;
	}

	
	public ProductDTODataModel getProductDTODataModel() {
		return productDTODataModel;
	}

	
	public void setProductDTODataModel(ProductDTODataModel productDTODataModel) {
		this.productDTODataModel = productDTODataModel;
	}

	public ProductDTO getClickedProductDTO() {
		return clickedProductDTO;
	}

	public void setClickedProductDTO(ProductDTO clickedProductDTO) {
		this.clickedProductDTO = clickedProductDTO;
	}

	
	public HotelDTODataModel getHotelDTODataModel() {
		return hotelDTODataModel;
	}

	public void setHotelDTODataModel(HotelDTODataModel hotelDTODataModel) {
		this.hotelDTODataModel = hotelDTODataModel;
	}

	public FlightDTODataModel getFlightDTODataModel() {
		return flightDTODataModel;
	}

	public void setFlightDTODataModel(FlightDTODataModel flightDTODataModel) {
		this.flightDTODataModel = flightDTODataModel;
	}

	public ExcursionDTODataModel getExcursionDTODataModel() {
		return excursionDTODataModel;
	}

	public void setExcursionDTODataModel(ExcursionDTODataModel excursionDTODataModel) {
		this.excursionDTODataModel = excursionDTODataModel;
	}

	public List<ProductDTO> getPackageProducts() {
		return packageProducts;
	}

	public void setPackageProducts(List<ProductDTO> packageProducts) {
		this.packageProducts = packageProducts;
	}

	public List<FlightDTO> getPackageFlights() {
		return packageFlights;
	}

	public void setPackageFlights(List<FlightDTO> packageFlights) {
		this.packageFlights = packageFlights;
	}

	public List<HotelDTO> getPackageHotels() {
		return packageHotels;
	}

	public void setPackageHotels(List<HotelDTO> packageHotels) {
		this.packageHotels = packageHotels;
	}

	public List<ExcursionDTO> getPackageExcursion() {
		return packageExcursion;
	}

	public void setPackageExcursion(List<ExcursionDTO> packageExcursion) {
		this.packageExcursion = packageExcursion;
	}

	

}
